package com.ruoyi.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.ruoyi.business.domain.bo.PersonFileBo;
import com.ruoyi.business.domain.vo.PersonFileVo;
import com.ruoyi.business.service.IPersonFileService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 人员档案类型
 *
 * @author xxyd
 * @date 2024-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/personFile")
public class PersonFileController extends BaseController {

    private final IPersonFileService iPersonFileService;

    /**
     * 查询人员档案类型列表
     */
    @SaCheckPermission("business:personFile:list")
    @GetMapping("/list")
    public TableDataInfo<PersonFileVo> list(PersonFileBo bo, PageQuery pageQuery) {
        return iPersonFileService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出人员档案类型列表
     */
    @SaCheckPermission("business:personFile:export")
    @Log(title = "人员档案类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PersonFileBo bo, HttpServletResponse response) {
        List<PersonFileVo> list = iPersonFileService.queryList(bo);
        ExcelUtil.exportExcel(list, "人员档案类型", PersonFileVo.class, response);
    }

    /**
     * 获取人员档案类型详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:personFile:query")
    @GetMapping("/{id}")
    public R<PersonFileVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iPersonFileService.queryById(id));
    }

    /**
     * 新增人员档案类型
     */
    @SaCheckPermission("business:personFile:add")
    @Log(title = "人员档案类型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PersonFileBo bo) {
        return toAjax(iPersonFileService.insertByBo(bo));
    }

    /**
     * 修改人员档案类型
     */
    @SaCheckPermission("business:personFile:edit")
    @Log(title = "人员档案类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PersonFileBo bo) {
        return toAjax(iPersonFileService.updateByBo(bo));
    }

    /**
     * 删除人员档案类型
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:personFile:remove")
    @Log(title = "人员档案类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iPersonFileService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 获取部门树列表
     */
    @GetMapping("/tree")
    public R<List<Tree<Long>>> personFileTree(Long personId) {
        return R.ok(iPersonFileService.personFileTree(personId));
    }
}
